package com.smasher.pictures;

import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;


/**
 * @author HXZ_Android
 */
public class PictureSelectDialog extends BottomSheetDialogFragment implements View.OnClickListener {

    private static final String TAG = "PictureSelect";

    private static final String ARG_PARAM = "param";

    @Mode
    private int mode = CAMERA_GALLERY;

    private ItemClickListener mItemClickListener;

    /**
     * 顶部向下偏移量
     */
    private final int topOffset = 0;
    private BottomSheetBehavior<FrameLayout> behavior;


    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param mode mode support key in {@link #CAMERA_GALLERY},
     *             {@link #CAMERA_ONLY} or {@link #GALLERY_ONLY}
     * @return A new instance of fragment PictureSelectDialog.
     */
    public static PictureSelectDialog newInstance(@Mode int mode) {
        PictureSelectDialog fragment = new PictureSelectDialog();
        Bundle args = new Bundle();
        args.putInt(ARG_PARAM, mode);
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mode = getArguments().getInt(ARG_PARAM);
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.layout_picture_select, container, false);
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        TextView mTvTitle = view.findViewById(R.id.tvTitle);
        TextView mTvText1 = view.findViewById(R.id.tv_text1);
        TextView mTvText2 = view.findViewById(R.id.tv_text2);
        TextView mTvCancel = view.findViewById(R.id.tvCancel);

        LinearLayout mItemContent1 = view.findViewById(R.id.item_content1);
        LinearLayout mItemContent2 = view.findViewById(R.id.item_content2);
        mItemContent2.setVisibility(View.GONE);

        mTvText1.setText(R.string.action_take_photo);
        mTvText2.setText(R.string.action_open_gallery);
        mTvTitle.setText(R.string.title_select);
        mTvCancel.setText(R.string.action_cancel);

        switch (mode) {
            case CAMERA_ONLY:
                mItemContent1.setVisibility(View.VISIBLE);
                mItemContent2.setVisibility(View.GONE);
                break;
            case GALLERY_ONLY:
                mItemContent1.setVisibility(View.GONE);
                mItemContent2.setVisibility(View.VISIBLE);
                break;
            case CAMERA_GALLERY:
            default:
                mItemContent1.setVisibility(View.VISIBLE);
                mItemContent2.setVisibility(View.VISIBLE);
                break;
        }

        mTvCancel.setOnClickListener(this);
        mItemContent1.setOnClickListener(this);
        mItemContent2.setOnClickListener(this);
    }


    @Override
    public void onStart() {
        super.onStart();
        BottomSheetDialog dialog = (BottomSheetDialog) getDialog();
        if (dialog != null) {
            behavior = dialog.getBehavior();
        }
    }


    public BottomSheetBehavior<FrameLayout> getBehavior() {
        return behavior;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        return new BottomSheetDialog(requireContext(), R.style.DefinedBottomSheetStyle);
    }

    @Override
    public void onClick(View v) {
        Log.d(TAG, "onClick: ");
        int id = v.getId();
        if (id == R.id.item_content1) {
            if (mItemClickListener != null) {
                mItemClickListener.onCamera();
            }
        } else if (id == R.id.item_content2) {
            if (mItemClickListener != null) {
                mItemClickListener.onGallery();
            }
        } else if (id == R.id.tvCancel) {
            if (mItemClickListener != null) {
                mItemClickListener.cancel();
            }
        }
        dismiss();
    }


    public interface ItemClickListener {

        void onCamera();

        void onGallery();

        void cancel();
    }


    public void setItemClickListener(ItemClickListener mItemClickListener) {
        this.mItemClickListener = mItemClickListener;
    }

    public static final int CAMERA_GALLERY = 0;
    public static final int CAMERA_ONLY = 1;
    public static final int GALLERY_ONLY = 2;

    @IntDef({CAMERA_GALLERY, CAMERA_ONLY, GALLERY_ONLY})
    @Retention(RetentionPolicy.SOURCE)
    @interface Mode {
    }
}
